package com.yhn.init.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.constant.MQConstant;
import com.yhn.init.exception.BusinessException;
import com.yhn.init.mapper.BoardsMapper;
import com.yhn.init.mapper.HardwareMapper;
import com.yhn.init.mapper.OrdersMapper;
import com.yhn.init.model.dto.boards.BoardsAddRequest;
import com.yhn.init.model.dto.orders.OrdersDesignRequest;
import com.yhn.init.model.entity.Boards;
import com.yhn.init.model.entity.Hardware;
import com.yhn.init.model.entity.Orders;
import com.yhn.init.model.enums.OrderStatusEnum;
import com.yhn.init.service.DesignService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yhn
 * @version 1.0
 * @description
 * @date 2024/3/20 19:39
 */
@Service
public class DesignServiceImpl implements DesignService {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private BoardsMapper boardsMapper;
    @Resource
    private HardwareMapper hardwareMapper;
    @Resource
    private OrdersMapper ordersMapper;

    /**
     * 从队列中获取订单id
     *
     * @return 返回订单ID
     */
    public Long consumeOrderIdFromQueue() {
        Long orderId = Long.valueOf(String.valueOf(rabbitTemplate.receiveAndConvert(MQConstant.ORDER_QUEUE_NAME)));
        if (orderId == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "无法获取到订单的ID");
        }
        return orderId;
    }

    /**
     * 完善订单信息
     *
     * @param designRequest 完善请求
     * @return 订单ID
     */
    @Override
    @Transactional
    public Long submitDesign(OrdersDesignRequest designRequest) {
        if (designRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "提交设计信息为空");
        }
        List<BoardsAddRequest> boardsAddRequests = designRequest.getBoardsAddRequests();
        boardsAddRequests.forEach(boardsAddRequest -> {
            Boards boards = new Boards();
            boards.setOrderId(designRequest.getOrderId());
            BeanUtil.copyProperties(boardsAddRequest, boards);
            try {
                boardsMapper.insert(boards);
            } catch (Exception e) {
                // 处理异常，如回滚事务等
                throw new BusinessException(ErrorCode.DATABASE_ERROR, "插入boards信息失败");
            }
        });
        designRequest.getHardwareAddRequests().forEach(hardwareAddRequest -> {
            Hardware hardware = new Hardware();
            hardware.setOrderId(designRequest.getOrderId());
            BeanUtil.copyProperties(hardwareAddRequest, hardware);
            try {
                hardwareMapper.insert(hardware);
            } catch (Exception e) {
                // 处理异常，如回滚事务等
                throw new BusinessException(ErrorCode.DATABASE_ERROR, "插入hardware信息失败");
            }
        });
        Long orderId = designRequest.getOrderId();
        Orders orders = ordersMapper.selectById(orderId);
        orders.setOrderStatus(OrderStatusEnum.COMPLETED.getValue());
        return designRequest.getOrderId();
    }
}
